package com.mayisoftware.mayioa.business.plan.impl.controller;

import com.mayisoftware.mayioa.business.plan.api.domain.PlanClass;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanClassService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/plan/class")
public class PlanClassController extends BaseController{
    private String prefix = "plan/class";

    @Autowired
    IPlanClassService planClassService;

    @RequiresPermissions("plan:class:view")
    @GetMapping()
    public String planClass()
    {
        return prefix + "/class";
    }

    @RequiresPermissions("plan:class:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlanClass planClass)
    {
        startPage();
        List<PlanClass> list = planClassService.selectPlanClassList(planClass);
        return getDataTable(list);
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增课程
     * @param planClass 课程信息
     * @return
     */
    @RequiresPermissions("plan:class:add")
    @Log(title = "计划课程管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(PlanClass planClass)
    {
        return toAjax(planClassService.insertPlanClass(planClass));
    }


    /**
     * 修改课程
     * @param planClassId 课程id
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{planClassId}")
    public String edit(@PathVariable("planClassId") Long planClassId, ModelMap mmap)
    {
        mmap.put("planClass", planClassService.selectPlanClassById(planClassId));
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     * @param planClass 课程信息
     * @return
     */
    @RequiresPermissions("plan:class:edit")
    @Log(title = "计划课程管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(PlanClass planClass)
    {
        planClass.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(planClassService.updatePlanClass(planClass));
    }

    @RequiresPermissions("plan:class:remove")
    @Log(title = "计划课程管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try{
            return toAjax(planClassService.deletePlanClassByIds(ids));
        }catch (Exception e){
            return toAjax(0);
        }
    }
}
